﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;


namespace DAO
{
    public class KhachHangDAO
    {
        public static List<KhachHangDTO> SelectAll()
        {
            List<KhachHangDTO> list = new List<KhachHangDTO>();
            String strSql = "select * from KhachHang";

            try
            {
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    KhachHangDTO kh = new KhachHangDTO();
                    kh.MaKH = int.Parse(dr["MaKH"].ToString());
                    kh.TenKH = dr["HoTen"].ToString();
                    kh.GioiTinh = dr["GioiTinh"].ToString();
                    kh.NgaySinh = dr["NgaySinh"].ToString();
                    kh.DiaChi = dr["DiaChi"].ToString();
                    kh.SoDienThoai = dr["SoDienThoai"].ToString();
                    list.Add(kh);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static List<KhachHangDTO> Select_by_Name(string TenKH)
        {
            List<KhachHangDTO> list = new List<KhachHangDTO>();
            String strSql = "select * from KhachHang where HoTen like N'%"+TenKH    +"%'";

            try
            {
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    KhachHangDTO kh = new KhachHangDTO();
                    kh.MaKH = int.Parse(dr["MaKH"].ToString());
                    kh.TenKH = dr["HoTen"].ToString();
                    kh.GioiTinh = dr["GioiTinh"].ToString();
                    kh.NgaySinh = dr["NgaySinh"].ToString();
                    kh.DiaChi = dr["DiaChi"].ToString();
                    kh.SoDienThoai = dr["SoDienThoai"].ToString();
                    list.Add(kh);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static bool Insert(KhachHangDTO kh)
        {
            bool result = false;

            string strSQLEmployee = "Insert into KhachHang(HoTen,GioiTinh,NgaySinh,DiaChi,SoDienThoai) " +
                "values (@HoTen,@GioiTinh,@NgaySinh,@DiaChi,@SoDienThoai)";

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@HoTen", kh.TenKH));
                sqlParams.Add(new SqlParameter("@GioiTinh", kh.GioiTinh));
                sqlParams.Add(new SqlParameter("@NgaySinh", kh.NgaySinh));
                sqlParams.Add(new SqlParameter("@DiaChi", kh.DiaChi));
                sqlParams.Add(new SqlParameter("@SoDienThoai", kh.SoDienThoai));
                // Call execute none query

                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);//.ExecuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool Delete(int MaKH)
        {
            bool result = false;
            string strSQLEmployee = "DELETE FROM KhachHang WHERE MaKH=@ID";
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ID", MaKH));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool Update(KhachHangDTO kh)
        {
            bool result = false;

            string strSQLEmployee = "Update KhachHang set HoTen = @Ten, GioiTinh=@GioiTinh, NgaySinh=@NgaySinh, DiaChi  =@DiaChi, SoDienThoai=@SoDienThoai " +
                "Where MaKH = @MaKH";

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaKH", kh.MaKH));
                sqlParams.Add(new SqlParameter("@Ten", kh.TenKH));
                sqlParams.Add(new SqlParameter("@GioiTinh", kh.GioiTinh));
                sqlParams.Add(new SqlParameter("@NgaySinh", kh.NgaySinh));
                sqlParams.Add(new SqlParameter("@DiaChi", kh.DiaChi));
                sqlParams.Add(new SqlParameter("@SoDienThoai", kh.SoDienThoai));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
